package com.zhkj.txg.module.home.adapter

import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.MarketCateListEntity
import com.zhkj.txg.module.home.entity.SellEntity
import com.zhkj.txg.module.home.ui.ProductListActivity
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import com.zhkj.txg.utils.GridItemDecoration
import com.zhkj.txg.utils.ImageUtils

class MarketListAdapter2 : BaseQuickAdapter<MarketCateListEntity, MarketCateViewHolder>(R.layout.item_market_list) {

    init {
        addChildClickViewIds(R.id.tvMarketCategoryMore)
        setOnItemChildClickListener { _, _, position ->
            ProductListActivity.categoryOpen(context, data[position].name, data[position].id)
        }
    }

    override fun convert(helper: MarketCateViewHolder, item: MarketCateListEntity) {
        helper.setText(R.id.tvMarketCategoryTitle, item.name)
        helper.adapter.replaceData(item.goods)
    }

    override fun onItemViewHolderCreated(viewHolder: MarketCateViewHolder, viewType: Int) {
        with(viewHolder.getView<RecyclerView>(R.id.rvMarket)) {
            layoutManager = GridLayoutManager(context, 3).apply {
                spanSizeLookup.isSpanIndexCacheEnabled = true
            }
            addItemDecoration(
                GridItemDecoration(
                    ScreenUtil.dip(context, 20),
                    ScreenUtil.dip(context, 10)
                )
            )
            adapter = viewHolder.adapter
        }
    }
}

class MarketCateViewHolder(view: View) : BaseViewHolder(view) {
    val adapter = MarketProductAdapter()
}

class MarketProductAdapter : BaseQuickAdapter<SellEntity, BaseViewHolder>(R.layout.item_home_goods) {

    init {
        setOnItemClickListener { _, _, position ->
            ProductDetailActivity.normalProduct(context, data[position].id)
        }
    }

    override fun convert(helper: BaseViewHolder, item: SellEntity) {
        val imageView = helper.setText(R.id.tvGoodsTitle, item.goodName)
            .setText(R.id.tvGoodsPrice, SpanUtil.setMoneySymbol(item.price,true))
            .setVisible(R.id.tvGoodsTag, TextUtils.equals(item.isNew, "1"))
            .getView<ImageView>(R.id.ivGoodsThumb)
        ImageUtils.loadImage(context, imageView, item.image)
        // 设置是显示市场价还是显示分享赚多少钱
        with(helper.getView<TextView>(R.id.tvSellSpread)) {
            if (AccountManager.isMember() && item.shareEarn.isNotEmpty() && item.shareEarn != "0") {
                setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                paint.flags = Paint.ANTI_ALIAS_FLAG
                text = SpanUtil.setPrefixMoneyStr(price = item.shareEarn)
            } else {
                setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                paint.isAntiAlias = true
                text = SpanUtil.setMoneySymbol(item.marketPrice)
            }
        }
    }
}